package com.example.boot.thread.demo.threaddemo.SynchronizedTest;

public class ThreadObjectCodeBlock implements  Runnable {

    static ThreadObjectCodeBlock thread= new ThreadObjectCodeBlock();
    Object o1 = new Object();
    Object o2 = new Object();
    @Override
    public void run() {
        synchronized (o1){
            System.out.println("我是对象锁代码块形式lock1，名字是："+Thread.currentThread().getName()+ " id是："+Thread.currentThread().getId());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+ "运行结束");
        }

        synchronized (o1){
            System.out.println("我是对象锁代码块形式lock2，名字是："+Thread.currentThread().getName()+ " id是："+Thread.currentThread().getId());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+ "运行结束");
        }

    }


    public static void main(String[] args) {
        Thread t1=new Thread(thread);
        Thread t2=new Thread(thread);
        t1.start();
        t2.start();
        while (t1.isAlive() || t2.isAlive()){

        }
        System.out.println("运行结束");
    }
}
